<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%!
  String myAttention(Integer uid, Input input) {
    Integer page = 1;
    Integer itemCount = 20;
    Integer groupId = input.getInt("group", -1); // 分组id, 默认全部分类问题
    String labelId = input.getDesc("label_id", "-1");
    Integer hospital = input.getInt("hospital", -1); // 医院id
    String sinceID = input.getDesc("since_id","0"); // 下拉刷新
    String maxID = input.getDesc("max_id","0"); // 上拉加载
    String keywords = input.getDesc("keywords", ""); // 上拉加载
    String table = "Quest as q, OperateHistory as o";
    Where where = new Where( groupId == -1?"":("q.groupId=" + groupId));
    where.and("q.id=o.oid").and("o.uid=", uid);
    if (!"-1".equals(labelId)) {
      table = table + ", LabelRelation as lr";
      where.and("lr.labelId in (" + labelId + ")").and("lr.targetId=q.id");
    }
    if (!"".equals(keywords)) {
      where.and(" q.title like '%" + keywords + "%' or q.content like '%" + keywords + "%'");
    }
    String order = "desc"; // 排序
    String questId = "";
    String selectContent = "new map(q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.cTime as create_time, q.aid as aid, q.groupId as group, o.id as oid)";
    if (!sinceID.equals("0")) {
      where.and("o.id > " + sinceID);
      order = "asc";
    } else if (!maxID.equals("0")) {
      where.and("o.id < " + maxID);
    }
    List<Map> questList = QBuilder.db("basedb").select(selectContent).from(table).where(where.and("q.hospital=", hospital).and("q.status = ", STATUS_ENABLE).and("o.status = ", STATUS_ENABLE).and("o.category=", QUEST_CATEGORY).and("o.operate=", ATTENTION)).order("o.id", order).page(itemCount, page).query().getResultObj();
    if (order.equals("asc")) { // 上拉加载时倒序
      Collections.reverse(questList);
    }
    completeQuestUserInfo(questList, hospital, uid);
    Message msg = new Message(0);
    if (isEmpty(questList))
        msg.setErrcodeAndErrmsg(30041, "没有更多了");
    msg.put("data", questList);
    return msg.toJson();
  }
%>
<%
	Tools.encryptPrint(out, myAttention(token.getUid(), input));
%>
